package com.changgou.order.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {
    //定义常量
    //添加积分任务交换机
    public static final String EX_BUYING_ADDPOINTUSER = "ex_buying_addpointuser";

    //添加积分消息队列
    public static final String CG_BUYING_ADDPOINT = "cg_buying_addpoint";

    //完成添加积分消息队列
    public static final String CG_BUYING_FINISHADDPOINT = "cg_buying_finishaddpoint";

    //添加积分路由key
    public static final String CG_BUYING_ADDPOINT_KEY = "addpoint";

    //完成添加积分路由key
    public static final String CG_BUYING_FINISHADDPOINT_KEY = "finishaddpoint";

    //配置交换机
    @Bean(EX_BUYING_ADDPOINTUSER)
    public Exchange EX_BUYING_ADDPOINTUSER(){
        return ExchangeBuilder.directExchange(EX_BUYING_ADDPOINTUSER).build();
    }
    //声明队列
    @Bean(CG_BUYING_ADDPOINT)
    public Queue CG_BUYING_ADDPOINT(){
        return new Queue(CG_BUYING_ADDPOINT);
    }

    @Bean(CG_BUYING_FINISHADDPOINT)
    public Queue CG_BUYING_FINISHADDPOINT(){
        return new Queue(CG_BUYING_FINISHADDPOINT);
    }

    //绑定队列到交换机
    @Bean
    public Binding BINDING_QUEUE_ADDPOINT(@Qualifier(CG_BUYING_ADDPOINT) Queue queue,
                                          @Qualifier(EX_BUYING_ADDPOINTUSER) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(CG_BUYING_ADDPOINT_KEY).noargs();
    }

    @Bean
    public Binding BINDING_QUEUE_FINISHADDPOINT(@Qualifier(CG_BUYING_FINISHADDPOINT) Queue queue,
                                          @Qualifier(EX_BUYING_ADDPOINTUSER) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(CG_BUYING_FINISHADDPOINT_KEY).noargs();
    }
    //订单支付成功消息队列
    public static final String ORDER_PAY = "order_pay";
    @Bean
    public Queue ORDER_PAY_QUEUE(){
        return new Queue(ORDER_PAY);
    }

    //死信队列订单支付超时
    public static final String QUEUE_ORDERCREATE = "queue.ordercreate";
    @Bean
    public Queue QUEUE_ORDERCREATE(){
        Map<String, Object> map = new HashMap<>();
        map.put("x-message-ttl","600000");
        map.put("x-dead-letter-exchange","exchange.ordertimeout");
        return QueueBuilder.durable(QUEUE_ORDERCREATE).withArguments(map).build();
    }

    public static final String EXCHANGE_ORDERTIMEOUT = "exchange.ordertimeout";
    public static final String QUEUE_ORDERTIMEOUT = "queue.ordertimeout";
    @Bean(QUEUE_ORDERTIMEOUT)
    public Queue QUEUE_ORDERTIMEOUT(){
        return new Queue(QUEUE_ORDERTIMEOUT);
    }
    @Bean(EXCHANGE_ORDERTIMEOUT)
    public Exchange EXCHANGE_ORDERTIMEOUT(){
        return ExchangeBuilder.fanoutExchange(EXCHANGE_ORDERTIMEOUT).durable(true).build();
    }

    @Bean
    public Binding EXCHANGE_ORDERTIMEOUT_BINDING(@Qualifier(QUEUE_ORDERTIMEOUT) Queue queue,@Qualifier(EXCHANGE_ORDERTIMEOUT) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }
}
